/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"github.com/valyala/fasthttp"

	. "saas_sys_base/modules/system/models"
	. "saas_sys_base/modules/system/service"
)

type SysMenuCtrl struct {
	service SysMenuService
}

func init() {
	vc := new(SysMenuCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *SysMenuCtrl) CtrlInfo() string {
	return "SysMenuCtrl"
}

func (vc *SysMenuCtrl) RoutInit() {
	// 列表页面
	lego.GET("/admin/SysMenu/listpage", vc.ListPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	lego.GET("/admin/SysMenu/listjson", vc.ListJson, lego.LEGO_SESSION_TYPE_ADMIN, false)
	// 编辑页面
	lego.GET("/admin/SysMenu/editpage", vc.GetEditPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	lego.POST("/admin/SysMenu/editpage", vc.PostEditPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	// 删除
	lego.POST("/admin/SysMenu/del", vc.DelRecode, lego.LEGO_SESSION_TYPE_ADMIN, false)
	// 子菜单列表及编辑
	lego.GET("/admin/SysMenu/SubMenu/listpage", vc.childsListPage, lego.LEGO_SESSION_TYPE_ADMIN, false)    // 列表页面
	lego.GET("/admin/SysMenu/SubMenu/editpage", vc.childsGetEditPage, lego.LEGO_SESSION_TYPE_ADMIN, false) // 详细页面
}

/* 列表接口
* params:
  ---
*/
func (vc *SysMenuCtrl) ListJson(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Keyword string // 关键字查询
			Pid     int64  `vd:"$>=0"` // 分页
			Page    int    `vd:"$>=0"` // 分页
			PSize   int    `vd:"$>0"`  // 分页大小
		}
		req := ReqParams{
			Keyword: string(ctxExt.Peek("keyword")),
			Pid:     utils.BytesToInt64(ctxExt.Peek("pid")),
			Page:    utils.BytesToInt(ctxExt.Peek("page")),
			PSize:   utils.BytesToInt(ctxExt.Peek("limit")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		// 业务查询
		if list, total, err := vc.service.ListSysMenu(req.Pid, req.Keyword, req.Page, req.PSize); err != nil {
			lego.LogError(err)
		} else {
			response["count"] = total
			response["data"] = list
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *SysMenuCtrl) ListPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		break
	}
	ctxExt.HTML("system/view/SysMenu/list.html", data)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *SysMenuCtrl) GetEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		type ReqParams struct {
			Id int64 // 记录id，为空则新建，否则为编辑
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}
		target := vc.service.GetSysMenuById(req.Id)
		data["detail"] = target
		// 列出所有1级菜单
		topmenus := vc.service.ListTopMenu()
		data["menus"] = topmenus
		break
	}
	ctxExt.HTML("system/view/SysMenu/edit.html", data)
	return
}

/* 数据编辑
* params:
  ---
*/
func (vc *SysMenuCtrl) PostEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""

	for {
		// 表单校验
		type ReqParams struct {
			Id int64 `vd:""`
			//IfDel   string `vd:""`
			//Cdate   string `vd:""`
			//Udate   string `vd:""`
			Pid     int64  `vd:""`
			Name    string `vd:"len($)>0"`
			Url     string `vd:""`
			MType   int    `vd:"$>0"`
			Show    int    `vd:"$>0"`
			SortIdx int    `vd:"$>=0"`
		}

		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
			//IfDel:   string(ctxExt.Peek("if_del")),
			//Cdate:   string(ctxExt.Peek("cdate")),
			//Udate:   string(ctxExt.Peek("udate")),
			Pid:     utils.BytesToInt64(ctxExt.Peek("pid")),
			Name:    string(ctxExt.Peek("name")),
			Url:     string(ctxExt.Peek("url")),
			MType:   utils.BytesToInt(ctxExt.Peek("m_type")),
			Show:    utils.BytesToInt(ctxExt.Peek("show")),
			SortIdx: utils.BytesToInt(ctxExt.Peek("sort_idx")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		// 业务处理
		recode := vc.service.GetSysMenuById(req.Id)
		if recode == nil {
			// 新建
			recode := new(SysMenu)
			recode.Id = req.Id
			//recode.IfDel = req.IfDel
			//recode.Cdate = req.Cdate
			//recode.Udate = req.Udate
			recode.Pid = req.Pid
			recode.Name = req.Name
			recode.Url = req.Url
			recode.MType = req.MType
			recode.Show = req.Show
			recode.SortIdx = req.SortIdx

			if false == vc.service.SaveSysMenu(recode) {
				code = 100
				msg = "false"
				break
			}
		} else {
			recode.Id = req.Id
			//recode.IfDel = req.IfDel
			//recode.Cdate = req.Cdate
			//recode.Udate = req.Udate
			recode.Pid = req.Pid
			recode.Name = req.Name
			recode.Url = req.Url
			recode.MType = req.MType
			recode.Show = req.Show
			recode.SortIdx = req.SortIdx

			if false == vc.service.UpdateSysMenu(recode) {
				code = 100
				msg = "false"
				break
			}
		}
		break
	}
	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}

/* 删除数据记录
* params:
  ---
*/
func (vc *SysMenuCtrl) DelRecode(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Id int64 `vd:"$>0"` // 记录id
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}
		// 业务删除
		target := vc.service.GetSysMenuById(req.Id)
		if target == nil {
			code = 100
			msg = "记录不存在"
			break
		}

		if false == vc.service.DelSysMenuById(target.Id) {
			code = 100
			msg = "操作失败"
			break
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *SysMenuCtrl) childsListPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}

	data := make(map[string]interface{})
	for {
		data["pid"] = string(ctxExt.Peek("pid"))
		break
	}

	ctxExt.HTML("system/view/SysMenu/childslist.html", data)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *SysMenuCtrl) childsGetEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		data["pid"] = string(ctxExt.Peek("pid"))
		id := utils.BytesToInt64(ctxExt.Peek("id"))
		target := vc.service.GetSysMenuById(id)
		data["detail"] = target
		break
	}
	ctxExt.HTML("system/view/SysMenu/childsedit.html", data)
	return
}
